package com.bjtu.restaurantreviews.controller;


import com.bjtu.restaurantreviews.dao.RestaurantRepository;
import com.bjtu.restaurantreviews.dao.TrackRepository;
import com.bjtu.restaurantreviews.domain.Result;
import com.bjtu.restaurantreviews.domain.Review;
import com.bjtu.restaurantreviews.domain.User;
import com.bjtu.restaurantreviews.service.UserService;
import com.bjtu.restaurantreviews.util.AliyunMessageUtil;
import com.bjtu.restaurantreviews.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping(value = "/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    TrackRepository trackRepository;

    @Autowired
    private RestaurantRepository restaurantRepository;

    //注册
    @PostMapping(value = "/reg")
    public Result reg(@RequestParam("checkcode") String checkcode, User user, HttpSession session) throws Exception{
        userService.reg(checkcode,user,session);
        return ResultUtil.sucess();
    }

    @GetMapping(value = "/test")
    public String test(){
        return "我是duang";
    }

    //登录
    @GetMapping(value = "/login")
    public Result login(@Valid User user, BindingResult bindingResult)throws Exception{
        if (bindingResult.hasErrors()){
            return ResultUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }else {
            return ResultUtil.sucess( userService.userLogin(user));
        }
    }
    //获取短信验证码
    @GetMapping(value = "/sms")
    public Result sms(HttpServletRequest request) throws Exception{
        String uphone = request.getParameter("uphone");
        String randomNum = AliyunMessageUtil.createRandomNum(6);
        request.getSession().setAttribute("uphone",uphone);
        request.getSession().setAttribute("randomNum",randomNum);
        request.getSession().setAttribute("codetime",new Date().getTime());
        AliyunMessageUtil.sendMsg(uphone,randomNum);
        return ResultUtil.sucess();
    }
    //重置密码
    @PutMapping(value = "/reset")
    public Result reset(@RequestParam("checkcode") String checkcode, User user, HttpSession session)throws Exception{
        userService.reset(checkcode,user,session);
        return ResultUtil.sucess();
    }
    //查询餐馆
    @GetMapping(value = "/inquire")
    public Result Inquire(@RequestParam(value = "word",required = false) String word,@RequestParam("pagenum") int pagenum)throws Exception{
        return ResultUtil.sucess(userService.inquire(word,pagenum));
    }

    //过滤器查询
    @GetMapping(value = "/filterinquire")
    public Result filter(@RequestParam(value = "type",required = false)String type,@RequestParam(value = "city",required = false)String city,
                         @RequestParam(value = "word",required = false)String word,@RequestParam(value = "pagenum") int pagenum,
                         @RequestParam(value = "sort",required = false)String sort) throws Exception{
        return ResultUtil.sucess(userService.filterquery(type,city,word,sort,pagenum));
    }

    //增加或更新轨迹信息
    @PostMapping(value = "/track")
    public Result track(@RequestParam(value = "tuid",required = false) Integer tuid,
                        @RequestParam(value = "trid",required = false) Integer trid) {
        if(userService.query(tuid,trid).isEmpty())
            return ResultUtil.sucess(userService.trackinsert(tuid,trid));
        else {
            userService.trackupdate(tuid, trid);
            return ResultUtil.sucess();
        }
    }

    //推荐前4个餐馆
    @GetMapping(value = "/like")
    public Result like(@RequestParam(value = "tuid",required = false) Integer tuid) {
        return ResultUtil.sucess(userService.like(tuid));
    }

    //餐馆评论信息
    @GetMapping(value = "/review")
    public Result review(@RequestParam(value = "rid",required = false)int rid,@RequestParam(value = "pagenum") int pagenum,
                         @RequestParam(value = "sort",required = false)String sort){
        return ResultUtil.sucess(userService.review(rid,pagenum,sort));
    }

    //餐馆详情推荐
    @GetMapping(value = "/recommendtype")
    public Result recommendtype(@RequestParam(value = "rtype",required = false) String rtype) {
        return ResultUtil.sucess(userService.recommendtype(rtype));
    }

    //广告餐馆
    @GetMapping(value = "/advertisement")
    public Result advertisement() {
        return ResultUtil.sucess(userService.advertisement());
    }

    //用户详细信息
    @GetMapping(value = "/userinfo")
    public Result userinfo(@RequestParam(value = "uid",required = false) Integer uid) {
        return ResultUtil.sucess(userService.userinfo(uid));
    }

    //推荐用户常去两种类型餐馆
    @GetMapping(value = "/selecttype")
    public Result selecttype(@RequestParam(value = "tuid", required = false) Integer tuid) {
        return ResultUtil.sucess(userService.selectType(tuid));
    }

    //用户查看自己评论
    @GetMapping(value = "/reviewinfo")
    public Result reviewinfo(@RequestParam(value = "revuid",required = false) Integer revuid,@RequestParam(value = "pagenum") Integer pagenum) {
        return ResultUtil.sucess(userService.reviewinfo(revuid,pagenum));
    }

    //推荐Spark计算结果
    @GetMapping(value = "/sparkrecommend")
    public Result sparkrecommend(@RequestParam(value = "uid") Integer uid) throws Exception{
        return ResultUtil.sucess(userService.sparkrecommend(uid));
    }

    //用户写评论
    @PostMapping(value = "/writereview")
    public Result writereview(Review review)throws Exception{
        userService.writereview(review);
        return ResultUtil.sucess();
    }

    //餐馆详细信息
    @GetMapping(value = "/restaurantinfo")
    public Result restaurantinfo(@RequestParam(value = "rid",required = false) Integer rid) {
        return ResultUtil.sucess(userService.restaurantinfo(rid));
    }

    //用户昵称及头像
    @GetMapping(value = "cmt_uinfo")
    public Result cmt_uinfo(@RequestParam(value = "cuid",required = false) String[] cuid) {
        return ResultUtil.sucess(userService.cmt_uinfo(cuid));
    }
}
